

/*/

PAPER: COVID-19 and economic preferences: 
evidence from a panel of cab drivers
  
   
 Data
------
Project uses data from baseline and follow up (online) survey

 

 */

 local users = "faragons"
 

global folder_input = /*set folder with dataset */ 
global folder_output = /*set folder to store results and figures   */  

	 set scheme stcolor_alt

cd "$folder_input"
 
use survey_data.dta, clear
   keep if   f_grupo_tratamiento==0

{
 /*
 *notes 
 ****************
 f_muestra==0    /* Identify sample with baseline and follow up data. = panel, aprox 1280 individuals */
 f_grupo_tratamiento     /* idntify control and treated group in Cabify experiment */
 
 */
 
 *Secundaria completa or less 1
 *tertiary incompelte 2 
 *tertiary complete 3
 gen educ_cat=.
 replace educ_cat=1 if nivel_educ<=5
  replace educ_cat=2 if nivel_educ== 6 | nivel_educ== 8
    replace educ_cat=3 if nivel_educ== 7 | nivel_educ== 9 |   nivel_educ==10
	

	gen edad2=edad*edad
	
	cap drop over?0
	 gen over40=(edad>40)
		 gen over50=(edad>=50)

	 cap drop edad_cat
	gen edad_cat=1 if edad<30
	replace edad_cat=2 if edad>=30 & edad<50
 	 replace edad_cat=3 if edad>=50  
	

			 cap drop edad_cat5yt
	gen edad_cat5yt=1 if edad<30
	replace edad_cat5yt=2 if edad>=30 & edad<35
	 replace edad_cat5yt=3 if edad>=35 & edad<40
	 	 replace edad_cat5yt=4 if edad>=40 & edad<45
 	 replace edad_cat5yt=5 if edad>=45 & edad<50
	 	 replace edad_cat5yt=6 if edad>=50 & edad<55
	 	 replace edad_cat5yt=7 if edad>=55 & edad<60
	 	 replace edad_cat5yt=8 if edad>=60 & edad<65

	 replace edad_cat5yt=9 if edad>=65  
	 
	 
 gen married_union=(estado_civil==2 | estado_civil==3)
 
 gen has_secocup=( diassem_trab_os!=.)
 




 *MODULO IX. AVERSIÓN AL RIESGO (Falk et al., 2016)
 * referred as "unfolding brackets" method or "staircase method" in psicology
 {
// The scale goes from 1 to 32, the lower (higher) the scale, the more risk averse (lover) the individual

foreach 	wayta in riskav f_riskav {
		* First arm: if riskav_1 == 2 (1-16)
gen 		`wayta'risk = 1 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==2 & `wayta'_7==2 & `wayta'_9==2 
replace 	`wayta'risk = 2 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==2 & `wayta'_7==2 & `wayta'_9==1
replace 	`wayta'risk = 3 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==2 & `wayta'_7==1 & `wayta'_8 == 2
replace 	`wayta'risk = 4 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==2 & `wayta'_7==1 & `wayta'_8 == 1

replace 	`wayta'risk = 5 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==1 & `wayta'_4 == 2 & `wayta'_6 == 2
replace 	`wayta'risk = 6 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==1 & `wayta'_4 == 2 & `wayta'_6 == 1
replace 	`wayta'risk = 7 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==1 & `wayta'_4 == 1 & `wayta'_5 == 2
replace 	`wayta'risk = 8 	if `wayta'_1 == 2 & `wayta'_2 == 2 & `wayta'_3==1 & `wayta'_4 == 1 & `wayta'_5 == 1

replace 	`wayta'risk = 9 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 2 & `wayta'_11 == 2 & `wayta'_12 == 2
replace 	`wayta'risk = 10 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 2 & `wayta'_11 == 2 & `wayta'_12 == 1
replace 	`wayta'risk = 11 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 2 & `wayta'_11 == 1 & `wayta'_13 == 2
replace 	`wayta'risk = 12 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 2 & `wayta'_11 == 1 & `wayta'_13 == 1

replace 	`wayta'risk = 13 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 1 & `wayta'_14 == 2 & `wayta'_16 == 2
replace 	`wayta'risk = 14 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 1 & `wayta'_14 == 2 & `wayta'_16 == 1
replace 	`wayta'risk = 15 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 1 & `wayta'_14 == 1 & `wayta'_15 == 2
replace 	`wayta'risk = 16 	if `wayta'_1 == 2 & `wayta'_2 == 1 & `wayta'_10 == 1 & `wayta'_14 == 1 & `wayta'_15 == 1

		* Second arm: if riskav_1 == 1 (17-32)
replace 	`wayta'risk = 17 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 2 & `wayta'_19 == 2 & `wayta'_21 == 2
replace 	`wayta'risk = 18 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 2 & `wayta'_19 == 2 & `wayta'_21 == 1
replace 	`wayta'risk = 19 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 2 & `wayta'_19 == 1 & `wayta'_20 == 2
replace 	`wayta'risk = 20 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 2 & `wayta'_19 == 1 & `wayta'_20 == 1

replace 	`wayta'risk = 21 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 1 & `wayta'_22 == 2 & `wayta'_24 == 2
replace 	`wayta'risk = 22 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 1 & `wayta'_22 == 2 & `wayta'_24 == 1
replace 	`wayta'risk = 23 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 1 & `wayta'_22 == 1 & `wayta'_23 == 2
replace 	`wayta'risk = 24 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_18 == 1 & `wayta'_22 == 1 & `wayta'_23 == 1

replace 	`wayta'risk = 25 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 2 & `wayta'_26 == 2 & `wayta'_28 == 2
replace 	`wayta'risk = 26 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 2 & `wayta'_26 == 2 & `wayta'_28 == 1
replace 	`wayta'risk = 27 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 2 & `wayta'_26 == 1 & `wayta'_27 == 2
replace 	`wayta'risk = 28 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 2 & `wayta'_26 == 1 & `wayta'_27 == 1

replace 	`wayta'risk = 29 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 1 & `wayta'_29 == 2 & `wayta'_30 == 2
replace 	`wayta'risk = 30 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 1 & `wayta'_29 == 2 & `wayta'_30 == 1
replace 	`wayta'risk = 31 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 1 & `wayta'_29 == 1 & `wayta'_31 == 2
replace 	`wayta'risk = 32 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_25 == 1 & `wayta'_29 == 1 & `wayta'_31 == 1
}		

rename 		riskavrisk risk 
rename 		f_riskavrisk f_risk

*fre 		risk if tiene_baseline==1		// One missing: The last arm was asked incorrectly
			label var risk "Escala de aversión al riesgo [+ alto + amante al riesgo] (base)"
*fre 		f_risk if tiene_followup==1		// One missing: The last arm was asked incorrectly
			label var f_risk "Escala de aversión al riesgo (cierre)"
gen 		c_risk=(f_risk-risk) if f_risk!=. & risk!=.
			replace c_risk=-1 if c_risk<0 & c_risk!=.
			replace c_risk=1 if c_risk>0 & c_risk!=.
			label var c_risk "Cambio en escala de aversión al riesgo"

tab 		c_risk, gen(_c_risk)
lab 		var _c_risk1 "\quad Disminuyó (+ averso al riesgo)"
lab 		var _c_risk2 "\quad Igual"
lab 		var _c_risk3 "\quad Aumentó (+ amante al riesgo)"
			
*fre 		auto_riskav f_auto_riskav
			label var auto_riskav "Aversión al riesgo autorreportada [+ alto + amante al riesgo] (base)"
			label var f_auto_riskav "Aversión al riesgo autorreportada (cierre)"
gen 		c_auto_riskav=(f_auto_riskav-auto_riskav) if f_auto_riskav!=. & auto_riskav!=.
			replace c_auto_riskav=-1 if c_auto_riskav<0 & c_auto_riskav!=.
			replace c_auto_riskav=1 if c_auto_riskav>0 & c_auto_riskav!=.
			label var c_auto_riskav "Cambio en aversión al riesgo autorreportada"
	
tab 		c_auto_riskav, gen(_c_auto_riskav)
lab 		var _c_auto_riskav1 "\quad Disminuyó"
lab 		var _c_auto_riskav2 "\quad Igual"
lab 		var _c_auto_riskav3 "\quad Aumentó"

*notes (Baseline)
*there is 1 case of wrong sequence of question (asked question 8 instead of q9 in baseline
*this case is coded as missing..
*no cases like that in follow up...
*I cannot find other cases of multiple switching in baseline nor follow up



*Preference index (as in falk et al QJE 2018 paper appendix AI p. 20)
*------------------------------

*Pbtain z scores for individual measures (based on mean and SD of the baseline)

foreach x of varlist   risk auto_riskav   {
sum  `x' if f_muestra==0 & risk!=. & f_risk!=.
cap drop z_`x' z_f_`x'

cap gen z_`x' = (`x' -r(mean) ) / r(sd)
cap gen z_f_`x' = (f_`x' -r(mean) ) / r(sd)

 }
 
 *Index uses same weights as QJE 2018 paper
 cap drop risk_index f_risk_index
 gen risk_index		= 	0.4729985*z_risk 	+ 0.5270015*z_auto_riskav
  gen f_risk_index	=	0.4729985*z_f_risk 	+ 0.5270015*z_f_auto_riskav
  
}
 		
		*NOTE: results similar if normalize using each period mean and SD instead of baseline year)
		
**************************

* MODULO X. PREFERENCIAS DE TIEMPO (Falk et al., 2016)
// 	The scale goes from 1 to 32, the lower (higher) the scale, the less (more) patient the individual.
{
foreach 	wayta in timepr f_timepr {
			* First arm: if timepr_1 == 2 (17-32)
gen 		`wayta'patience = 32 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==2 & `wayta'_4==2 & `wayta'_5==2 
replace 	`wayta'patience = 31 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==2 & `wayta'_4==2 & `wayta'_5==1 
replace 	`wayta'patience = 30 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==2 & `wayta'_4==1 & `wayta'_6 == 2
replace 	`wayta'patience = 29 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==2 & `wayta'_4==1 & `wayta'_6 == 1

replace 	`wayta'patience = 28 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==1 & `wayta'_7 == 2 & `wayta'_9 == 2
replace 	`wayta'patience = 27 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==1 & `wayta'_7 == 2 & `wayta'_9 == 1
replace 	`wayta'patience = 26 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==1 & `wayta'_7 == 1 & `wayta'_8 == 2
replace 	`wayta'patience = 25 	if `wayta'_1 == 2 & `wayta'_2==2 & `wayta'_3==1 & `wayta'_7 == 1 & `wayta'_8 == 1

replace 	`wayta'patience = 24 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 2 & `wayta'_11 == 2 & `wayta'_12 == 2
replace 	`wayta'patience = 23 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 2 & `wayta'_11 == 2 & `wayta'_12 == 1
replace 	`wayta'patience = 22 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 2 & `wayta'_11 == 1 & `wayta'_13 == 2
replace 	`wayta'patience = 21 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 2 & `wayta'_11 == 1 & `wayta'_13 == 1

replace 	`wayta'patience = 20 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 1 & `wayta'_14 == 2 & `wayta'_15 == 2
replace 	`wayta'patience = 19 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 1 & `wayta'_14 == 2 & `wayta'_15 == 1
replace 	`wayta'patience = 18 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 1 & `wayta'_14 == 1 & `wayta'_16 == 2
replace 	`wayta'patience = 17 	if `wayta'_1 == 2 & `wayta'_2==1 & `wayta'_10 == 1 & `wayta'_14 == 1 & `wayta'_16 == 1
		* Second arm: if timepr_1 == 1 (1-16)
replace 	`wayta'patience = 16 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 2 & `wayta'_26 == 2 & `wayta'_27 == 2
replace 	`wayta'patience = 15 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 2 & `wayta'_26 == 2 & `wayta'_27 == 1
replace 	`wayta'patience = 14 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 2 & `wayta'_26 == 1 & `wayta'_28 == 2
replace 	`wayta'patience = 13 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 2 & `wayta'_26 == 1 & `wayta'_28 == 1

replace 	`wayta'patience = 12 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 1 & `wayta'_29 == 2 & `wayta'_30 == 2
replace 	`wayta'patience = 11 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 1 & `wayta'_29 == 2 & `wayta'_30 == 1
replace 	`wayta'patience = 10 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 1 & `wayta'_29 == 1 & `wayta'_31 == 2
replace 	`wayta'patience = 9 	if `wayta'_1 == 1 & `wayta'_17 == 2 & `wayta'_25 == 1 & `wayta'_29 == 1 & `wayta'_31 == 1

replace 	`wayta'patience = 8 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 2 & `wayta'_19 == 2 & `wayta'_21 == 2
replace 	`wayta'patience = 7 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 2 & `wayta'_19 == 2 & `wayta'_21 == 1
replace 	`wayta'patience = 6 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 2 & `wayta'_19 == 1 & `wayta'_20 == 2
replace 	`wayta'patience = 5 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 2 & `wayta'_19 == 1 & `wayta'_20 == 1

replace 	`wayta'patience = 4 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 1 & `wayta'_22 == 2 & `wayta'_24 == 2
replace 	`wayta'patience = 3 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 1 & `wayta'_22 == 2 & `wayta'_24 == 1
replace 	`wayta'patience = 2 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 1 & `wayta'_22 == 1 & `wayta'_23 == 2
replace 	`wayta'patience = 1 	if `wayta'_1 == 1 & `wayta'_17 == 1 & `wayta'_18 == 1 & `wayta'_22 == 1 & `wayta'_23 == 1
}

rename 		timeprpatience patience
rename 		f_timeprpatience f_patience

*fre 		patience if tiene_baseline==1	
			label var patience "Escala de preferencias de tiempo [+ alto + paciente] (base)"
*fre 		f_patience if tiene_followup==1		
			label var f_patience "Escala de preferencias de tiempo (cierre)"
gen 		c_patience=(f_patience-patience) if f_patience!=. & patience!=.
			replace c_patience=-1 if c_patience<0 & c_patience!=.
			replace c_patience=1 if c_patience>0 & c_patience!=.
			label var c_patience "Cambio en escala de preferencias de tiempo"

tab 		c_patience, gen(_c_patience)
lab 		var _c_patience1 "\quad Disminuyó (- paciente)"
lab 		var _c_patience2 "\quad Igual"
lab 		var _c_patience3 "\quad Aumentó (+ paciente)"

*fre 		auto_timepr f_auto_timepr
			label var auto_timepr "Preferencia de tiempo autorreportada [+ alto + paciente] (base)"
			label var f_auto_timepr "Preferencia de tiempo autorreportada (cierre)"
gen 		c_auto_timepr=(f_auto_timepr-auto_timepr) if f_auto_timepr!=. & auto_timepr!=.
			replace c_auto_timepr=-1 if c_auto_timepr<0 & c_auto_timepr!=.
			replace c_auto_timepr=1 if c_auto_timepr>0 & c_auto_timepr!=.
			label var c_auto_timepr "Cambio en preferencia de tiempo autorreportada"

tab 		c_auto_timepr, gen(_c_auto_timepr)
lab 		var _c_auto_timepr1 "\quad Disminuyó"
lab 		var _c_auto_timepr2 "\quad Igual"
lab 		var _c_auto_timepr3 "\quad Aumentó"

*Preference index (as in falk et al QJE 2018 paper appendix AI p. 20)
*------------------------------

*Pbtain z scores for individual measures (based on mean and SD of the baseline)

foreach x of varlist   patience auto_timepr   {
sum  `x' if f_muestra==0 & patience!=. & f_patience!=.
cap gen z_`x' = (`x' -r(mean) ) / r(sd)
cap gen z_f_`x' = (f_`x' -r(mean) ) / r(sd)

 }
 
 
  
 *Index uses same weights as QJE 2018 paper
 gen patience_index		= 	0.7115185*z_patience	+ 0.2884815*z_auto_timepr
  gen f_patience_index	=	0.7115185*z_f_patience 	+ 0.2884815*z_f_auto_timepr
  
  
	}
	
	
*happines

*Pbtain z scores for individual measures (based on mean and SD of the baseline)

foreach x of varlist   feliz_hoy feliz_ayer   {
sum  `x' if f_muestra==0 
cap gen z_`x' = (`x' -r(mean) ) / r(sd)
cap gen z_f_`x' = (f_`x' -r(mean) ) / r(sd)

 }
 
  gen feliz_index		= 	0.5*z_feliz_hoy 	+ 0.5*z_feliz_ayer
  gen f_feliz_index	=	0.5*z_f_feliz_hoy 	+ 0.5*z_f_feliz_ayer
  


**anxiety (self reportes

foreach x of varlist   ansioso_ayer      {
sum  `x' if f_muestra==0
cap gen z_`x' = (`x' -r(mean) ) / r(sd)
cap gen z_f_`x' = (f_`x' -r(mean) ) / r(sd)

 }
 

 

*diff. variables
gen D_z_ansioso_ayer=z_f_ansioso_ayer - z_ansioso_ayer
gen D_feliz_index=f_feliz_index - feliz_index
gen D_risk =f_risk  -  risk 
gen D_patience=f_patience  - patience 
gen D_risk_index=f_risk_index -  risk_index
gen D_patience_index=f_patience_index - patience_index

gen D_rang_ing_hogar=f_rang_ing_hogar - rang_ing_hogar
 
  egen f_covid_HH=rmax(f_covid_familia_ud f_covid_familia_hog)
egen f_covid_nonHH=rmax(f_covid_familia_nonhog f_covid_familia_amist f_covid_familia_trabj  )

cap drop f_prob_financial
egen f_prob_financial=rmax( f_prob_perdempleo f_prob_quiebra f_prob_despidoreducc f_prob_reduciotrosingresos)
egen f_cov_prob_financial=rmax( f_cov_prob_perdempleo f_cov_prob_quiebra f_cov_prob_despidoreducc f_cov_prob_reduciotrosingresos)

**measures of health shocks
*1. suffered enfermedad grave, death o accident (no COVID)

gen prob_health_noCOVID=0 if f_prob_enfacc!=.
replace prob_health_noCOVID=1 if  f_prob_enfacc==1 & f_cov_prob_enfacc==0

*2. suffered enfermedad grave, death o accident (  COVID)
gen prob_health_COVID=0 if f_prob_enfacc!=.
replace prob_health_COVID=1 if  f_prob_enfacc==1 & f_cov_prob_enfacc==1

*3 HH member got COVID but no serious disease or death
gen prob_HH_COVID_noserious=0 if f_covid_HH!=.
replace prob_HH_COVID_noserious=1 if  f_covid_HH==1 & f_cov_prob_enfacc==0




} 


*create ubigeo
{
	gen ubigeo=.
replace ubigeo=150101 if distritores==1
replace ubigeo=150103 if distritores==2
replace ubigeo=150105 if distritores==5
replace ubigeo=150106 if distritores==6
replace ubigeo=150107 if distritores==7
replace ubigeo=150108 if distritores==8
replace ubigeo=150110 if distritores==10
replace ubigeo=150111 if distritores==11
replace ubigeo=150112 if distritores==12
replace ubigeo=150113 if distritores==13
replace ubigeo=150114 if distritores==14
replace ubigeo=150115 if distritores==15
replace ubigeo=150116 if distritores==16
replace ubigeo=150117 if distritores==17
replace ubigeo=150118 if distritores==18
replace ubigeo=150119 if distritores==19
replace ubigeo=150121 if distritores==21
replace ubigeo=150122 if distritores==22
replace ubigeo=150128 if distritores==28
replace ubigeo=150130 if distritores==30
replace ubigeo=150132 if distritores==32
replace ubigeo=150133 if distritores==33
replace ubigeo=150134 if distritores==34
replace ubigeo=150135 if distritores==35
replace ubigeo=150136 if distritores==36
replace ubigeo=150137 if distritores==37
replace ubigeo=150140 if distritores==40
replace ubigeo=150141 if distritores==41
replace ubigeo=150142 if distritores==42
replace ubigeo=150143 if distritores==43
replace ubigeo=70102 if distritores==71
replace ubigeo=70106 if distritores==76
}
*


tostring f_submissiondate , gen (f_date)  usedisplayformat force
gen f_year=substr(f_date,6,4)
gen f_month=substr(f_date,3,3)
gen f_day=substr(f_date,1,2)
replace f_month ="12" if f_month=="dec"
replace f_month ="1" if f_month=="jan"
replace f_month ="2" if f_month=="feb"


destring f_year f_month f_day, replace
gen f_year_month=f_year*100+f_month

*********
gen panel_sample=0
replace panel_sample=1 if risk!=.

cap gen female=(sexo==0)

cap gen couple=(estado_civil==2 |estado_civil==3)

 cap gen tertiary_complete=(nivel_educ==7 | nivel_educ==9 | nivel_educ==10   )
 
*gasto en salud

 egen tmpgasto=rsum(gasto_salud-gasto_alq)
gen share_exp_salud=gasto_salud/tmpgasto

gen has_followup=(f_risk!=.)
gen has_baseline=( risk!=.)




*create sample
		    reg D_risk_index      f_grupo_tratamiento if f_muestra==0  , r 
gen sample=e(sample)

cap drop dum?
	cap tab edad_cat, gen(dum)
 label var dum1 "20-29"
	label var dum2 "30-49"
		label var dum3 "50+"
		
cap drop z_edad  
		sum  edad if sample==1
cap gen z_edad = (edad -r(mean) ) / r(sd)

		
		
 
* TABLE 1 SUMMARY
*-------------------------------

tabstat female edad  tertiary_complete estudiante_ahora  couple n_hijos   seg_noseguro enfermedad  prob_HH_COVID_noserious   ///
      f_prob_enfacc  f_prob_financial	 f_prob_hechodelic  if sample==1, by(edad_cat)   stat(mean)
	  
tabstat risk_index patience_index feliz_index z_ansioso_ayer if sample==1, by(edad_cat)   stat(mean)
	
	tabstat  f_feliz_index z_f_ansioso_ayer if sample==1, by(edad_cat)   stat(mean)
	
	
 tab edad_cat if sample==1
 
	   
*FIGURE 1 
*-----------------------

    twoway (kdensity risk_index   if f_muestra==0,   lpattern(dash) )  (kdensity   f_risk_index if f_muestra==0 ) , ytitle("density") xtitle("")   legend(label (1 "Baseline (May 2019)") label(2 "Follow-up (Dec 2020-Feb 2021)") )
		graph export "$folder_output/fig_density_risk.png", replace
		
		
		twoway (kdensity patience_index   if f_muestra==0, lpattern(dash) )  (kdensity   f_patience_index if f_muestra==0 ) , ytitle("density")  xtitle("")  legend(label (1 "Baseline (May 2019)") label(2 "Follow-up (Dec 2020-Feb 2021)") )
		graph export "$folder_output/fig_density_time.png", replace

	
	*FIGURE 2
	***************
	
 	reg D_risk_index     dum?    $covar     if f_muestra==0, r nocons
coefplot , keep(dum?) yline(0) vert xtitle("Age group (years)") ytitle("{&Delta} risk-taking index")
			graph export "$folder_output/fig_impact_risk.png", replace

	
		reg D_patience_index     dum?    $covar     if f_muestra==0, r nocons
coefplot , keep(dum?) yline(0) vert xtitle("Age group (years)") ytitle("{&Delta} patience index")
		graph export "$folder_output/fig_impact_patience.png", replace

		
	  
	* TABLE 2 MAIN 
	***************

	
	 global covar = "f_feliz_index z_f_ansioso_ayer i.f_year_month "
	 	 global covar =  " D_feliz_index D_z_ansioso_ayer  i.f_year_month "
	*  enfermedad tertiary
	
		    reg D_risk_index      f_grupo_tratamiento $covar if f_muestra==0  , r 
			outreg2 using table_main.xls, bdec(3)  replace

			reg D_risk_index      dum?     f_grupo_tratamiento $covar if f_muestra==0, r nocons
					outreg2 using table_main.xls, bdec(3)   

reg D_risk_index    dum?            prob_HH_COVID_noserious         f_prob_enfacc f_prob_financial   f_prob_hechodelic     $covar      if f_muestra==0, r  nocons
					outreg2 using table_main.xls, bdec(3)   

					 * patience
	    reg D_patience_index      f_grupo_tratamiento $covar if f_muestra==0, r
						outreg2 using table_main.xls, bdec(3)   
	reg D_patience_index      dum?     f_grupo_tratamiento   $covar if f_muestra==0, r nocons
						outreg2 using table_main.xls, bdec(3) 
						
						
reg D_patience_index     dum?       prob_HH_COVID_noserious         f_prob_enfacc f_prob_financial    f_prob_hechodelic      $covar   if f_muestra==0, r nocons
					outreg2 using table_main.xls, bdec(3)   

 	 
	
	
	
	
 
 
 						
* TABLE A.1 Appendix  
	***************
 
 *risk taking decreases with AGE. consistenwith other studies....
 *patience is flat..
  reg  risk_index   z_edad             if f_muestra==0, r   
   outreg2 using table_age.xls, bdec(3)  replace

 reg  risk_index   z_edad     feliz_index z_ansioso_ayer  enfermedad tertiary      i.sex         if f_muestra==0, r   
    outreg2 using table_age.xls, bdec(3)   
 reg  patience_index   z_edad             if f_muestra==0, r   
    outreg2 using table_age.xls, bdec(3)   

 reg  patience_index   z_edad   feliz_index z_ansioso_ayer  enfermedad tertiary      i.sex         if f_muestra==0, r   
    outreg2 using table_age.xls, bdec(3)   

 
 
 